import {Message,MessageBox} from "element-ui"
import axios from 'axios'

import loading from  './loading'
import router from "../router"

// const baseURL = '/api'
// const baseURL = 'http://120.77.156.205:9804'

// 普通请求
const http = axios.create({
  // baseURL: baseURL,
  timeout: 18000,
  withCredentials: true
})

// 设备请求
export const DerviceHttp = axios.create({
  timeout: 18000,
  withCredentials: true
})


// 请求拦截器
http.interceptors.request.use(config => {
      config.headers = {
        token: `${sessionStorage.getItem('token')}`,
        xip: localStorage.getItem("Ip"),
        'Content-Type': 'application/json;charset=UTF-8',
      }
      // console.log(config.headers)
      loading.showLoding()
      return config
    },
    err => {
      return Promise.reject(err)
    }
)


// 设备请求拦截器
DerviceHttp.interceptors.request.use(config => {
  config.headers = {
    token: `${sessionStorage.getItem('DerviceToken')}`,
    'Content-Type': 'application/json;charset=UTF-8',
  }
  loading.showLoding()
  return config
},
err => {
  return Promise.reject(err)
}
)

function removetoken() {
    if(sessionStorage.getItem('token')) {
      Router.replace('/login')
      sessionStorage.removeItem('token')
      Message.info('鉴权失败')
    }
}

function removedervicetoken() {
  if(sessionStorage.getItem('DerviceToken')) {
    Router.replace('/login')
    sessionStorage.removeItem('DerviceToken')
    Message.info('鉴权失败')
  }
}

// 响应拦截器即异常处理
http.interceptors.response.use(response => {
    setTimeout(() => {
      loading.hideLoding()
    }, 300);
    let { statusCode } = response.data
    // token验证失败，重新登录
    if(statusCode === 20020) {
      removetoken()
    }

    if(statusCode === 20021) {
      Message.error('网关失败')
    }

    if (response) {
      return response
    }
  },
  err => {
    if (err && err.response) {
      switch (err.response.status) {
        case 400:
          err.message = "错误请求"
          break
        case 401:
          router.push('./login')
          sessionStorage.removeItem('token')
          window.location.reload()
          err.message = "未授权，请重新登录！"
          break
        case 403:
          err.message = "拒绝访问"
          break
        case 404:
          err.message = "请求错误,未找到该资源！"
          break
        case 405:
          err.message = "请求方法未允许"
          break
        case 408:
          err.message = "请求超时"
          break
        case 500:
          err.message = "服务器端出错"
          break
        case 501:
          err.message = "网络未实现"
          break
        case 502:
          err.message = "网络错误"
          break
        case 503:
          err.message = "服务不可用"
          break 
        case 504:
          err.message = "网络超时"
          break
        case 505:
          err.message = "http版本不支持该请求"
          break
        default:
          err.message = `连接错误${err.response.status}`
      }
    } else {
      err.message = "连接到服务器失败"
    }

    if (err && err.response) { 
      Message.error(err.message)
    }
    setTimeout(() => {
      loading.hideLoding()
    }, 300);
    return Promise.reject(err)
  }
)

// 设备响应拦截器即异常处理
DerviceHttp.interceptors.response.use(response => {
  setTimeout(() => {
    loading.hideLoding()
  }, 300);
  let { statusCode } = response.data
  // token验证失败，重新登录
  if(statusCode === 20020) {
    removedervicetoken()
  }

  if(statusCode === 20021) {
    Message.error('网关失败')
  }

  if (response) {
    return response
  }
},
err => {
  if (err && err.response) {
    switch (err.response.status) {
      case 400:
        err.message = "错误请求"
        break
      case 401:
        router.push('./login')
        sessionStorage.removeItem('token')
        window.location.reload()
        err.message = "未授权，请重新登录！"
        break
      case 403:
        err.message = "拒绝访问"
        break
      case 404:
        err.message = "请求错误,未找到该资源！"
        break
      case 405:
        err.message = "请求方法未允许"
        break
      case 408:
        err.message = "请求超时"
        break
      case 500:
        err.message = "服务器端出错"
        break
      case 501:
        err.message = "网络未实现"
        break
      case 502:
        err.message = "网络错误"
        break
      case 503:
        err.message = "服务不可用"
        break
      case 504:
        err.message = "网络超时"
        break
      case 505:
        err.message = "http版本不支持该请求"
        break
      default:
        err.message = `连接错误${err.response.status}`
    }
  } else {
    err.message = "连接到服务器失败"
  }

  if (err && err.response) { 
    Message.error(err.message)
  }
  setTimeout(() => {
    loading.hideLoding()
  }, 300);
  return Promise.reject(err)
}
)

export default http
